package com.example.imagesharing;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import android.widget.ImageView;

public class addActivity extends AppCompatActivity {
    private Button addShare;
    private ImageView addImg;
    String path;
    Uri uri;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add);
        addImg = findViewById(R.id.add_img);
        addShare = findViewById(R.id.add_button);
        final EditText addContent = findViewById(R.id.add_content);
        addImg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(Intent.ACTION_PICK, null);
                intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
                startActivityForResult(intent, 2);
            }
        });
        addShare.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                String content = addContent.getText().toString();
//                share(content);
               // Toast.makeText(mContext, mNames.get(position), Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 2) {
            // 从相册返回的数据
            if (data != null) {
                // 得到图片的全路径
                Uri uri = data.getData();
                addImg.setImageURI(uri);
            }
        }

    }
//    public static void shareImage(Context context, Uri uri, String title)
//    {
//        Intent intent = new Intent(Intent.ACTION_SEND);
//        intent.setType("image/png");
//        intent.putExtra(Intent.EXTRA_STREAM, uri);
//        context.startActivity(Intent.createChooser(intent, title));
//    }
//    private void share(String content, Uri uri){
//        Intent shareIntent = new Intent(Intent.ACTION_SEND);
//        if(uri!=null){
//            shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
//            shareIntent.setType("image/*");
//            //当⽤户选择短信时使⽤sms_body取得⽂字
//            shareIntent.putExtra("sms_body", content);
//        }else{
//            shareIntent.setType("text/plain");
//        }
//        shareIntent.putExtra(Intent.EXTRA_TEXT, content);
//        //⾃定义选择框的标题
//        //startActivity(Intent.createChooser(shareIntent, "邀请好友"));
//        //系统默认标题
//        startActivity(shareIntent);
//    }
    private void share(String content) {
        Intent share_intent = new Intent();
        share_intent.setAction(Intent.ACTION_SEND);                  //设置分享行为
        share_intent.setType("text/plain");                         //设置分享内容的类型
        share_intent.putExtra(Intent.EXTRA_SUBJECT, content);//添加分享内容标题
        share_intent = Intent.createChooser(share_intent, "分享");//创建分享的Dialog
        startActivity(share_intent);
    }


}







